import activeApi from '../../api/activeApi'
import {getLocation, chooseLocation} from '../../util/location'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressName: '',
    address: '',
    markers: [],
    longitude: '',
    latitude: ''
  },


  async getData() {
    this.setData({
      longitude: wx.getStorageSync('longitude'),
      latitude: wx.getStorageSync('latitude'),
    })
    let res = await activeApi.geoWithin({
      lat: wx.getStorageSync('latitude'),
      lng: wx.getStorageSync('longitude'),
    })

    let list = []

    for (let item of res) {

      list.push({
        id: item._id,
        latitude: item.location.latitude,
        longitude: item.location.longitude,
        name: item.address,
        iconPath: item.isPlace ? '/images/track-dot.png' : '/images/location.png',
        width: 23,
        height: 31,
        callout: {
          content: item.isPlace ? '钓鱼场' : '钓点',
          display: "ALWAYS",
          padding: 5,
          bgColor: '#6495ed',
          color: '#fff',
          fontSize: 20
        }
      })
    }
    this.setData({
      markers: list
    })

    this.mapCtx.includePoints({
      points: list,
      padding: [100, 30, 100, 30]
    })
  },
  onMarkerTap(e) {
    let id = e.detail.markerId
    wx.navigateTo({
      url: '/pages/activeDetail/activeDetail?activeId=' + id,
    })
  },

  async choose() {
    await chooseLocation()
    this.setData({
      address: wx.getStorageSync('address'),
      addressName: wx.getStorageSync('addressName'),
    })
    this.getData()
   
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.mapCtx = wx.createMapContext('myMap')
    await getLocation()
    this.setData({
      address: wx.getStorageSync('address'),
      addressName: wx.getStorageSync('addressName'),
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      selected: 1
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})